package world.criver.h50909.service;

import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import world.criver.h50909.dao.daoInitialize;
import world.criver.h50909.entity.entConfig;
import world.criver.h50909.properties.proConfig;
import world.criver.h50909.response.resBaseInfo;
import world.criver.h50909.response.resIni;
import world.criver.h50909.tool.toolConfig;
@Service
public class serInitialize implements daoInitialize {
    @Autowired
    private toolConfig toolConfig;
    @Autowired
    private proConfig proConfig;
    @Override
    public resIni ini(HttpServletRequest http) {
        entConfig con=toolConfig.query();
        String ipAddress = http.getHeader("X-Forwarded-For");
        if (ipAddress == null || ipAddress.isEmpty() || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = http.getHeader("Proxy-Client-IP");
        }
        if (ipAddress == null || ipAddress.isEmpty() || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = http.getHeader("WL-Proxy-Client-IP");
        }
        if (ipAddress == null || ipAddress.isEmpty() || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = http.getRemoteAddr();
        }
        resIni res=new resIni();
        res.setIp(ipAddress);
        if(con!=null){
            res.setCompany(con.getCompany());
            res.setLogo(con.getLogo());
        }
        return res;
    }

    @Override
    public resBaseInfo queryBaseInfo() {
        return new resBaseInfo(proConfig.getLogo(), proConfig.getCompany());
    }
}
